import 'package:flutter/material.dart';
import 'package:extended_text_field/extended_text_field.dart';
import 'package:test1/widget/voice/voice_widget.dart';

import 'emoji_text.dart';


/// 解决中文和英文提前自动换行问题
extension FixAutoLines on String {
  String fixAutoLines() {
    return Characters(this).join('\u{200B}');
  }
}



class BottomNavigationBarInput extends StatefulWidget {
  final Function biaoqing;
  final Function hiddenFucntion;
  final Function moreTap;
  final Function value;
  final Function stopRecord;
  final TextEditingController textEditingController;

  final GlobalKey<ExtendedTextFieldState> textFieldKey;

  const BottomNavigationBarInput(
      {Key? key,
      required this.biaoqing,
      required this.moreTap,
      required this.value,
      required this.textFieldKey,
      required this.textEditingController,
      required this.hiddenFucntion,
      required this.stopRecord})
      : super(key: key);

  @override
  State<BottomNavigationBarInput> createState() =>
      _BottomNavigationBarInputState();
}

class _BottomNavigationBarInputState extends State<BottomNavigationBarInput> {
  int type = 1;

  int biaoqing = 1;

  final _focusNode = FocusNode();

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width,
      padding: const EdgeInsets.symmetric(horizontal: 10),
      // color: Color(0xFFF6F6F6),
      child: Container(
        child: Row(
          children: [
            InkResponse(
              onTap: () {
                setState(() {
                  type = type == 1 ? 0 : 1;
                });
                widget.hiddenFucntion();
              },
              child: Row(
                children: [
                  Image.asset(
                    type == 0
                        ? "asset/message/keyboard.png"
                        : "asset/message/play_the_sound.png",
                    width: 24,
                    height: 24,
                    fit: BoxFit.fill,
                  ),
                  // Icon(widget.type == 0 ? Icons.keyboard : Icons.wifi_rounded),
                  SizedBox(width: 10),
                ],
              ),
            ),
            Expanded(
              flex: 3,
              child: type == 0
                  ? Container(
                      padding: EdgeInsets.only(left: 8, right: 1),
                      margin: EdgeInsets.symmetric(vertical: 7),
                      child: VoiceWidget(
                        height: 40,
                        stopRecord: (path, double audioTimeLength) {
                          widget.stopRecord(path, audioTimeLength);
                        },
                      ),
                    )
                  : Container(
                      constraints: BoxConstraints(minHeight: 40),
                      alignment: Alignment.center,
                      padding: EdgeInsets.only(left: 8, right: 1),
                      margin: EdgeInsets.symmetric(vertical: 7),
                      decoration: BoxDecoration(
                        color: Color(0xFFFFFFFF),
                        borderRadius: BorderRadius.circular(5),
                      ),
                      child: ExtendedTextField(
                        cursorColor: Color(0xFF68D231),
                        // cursorHeight: 30.0/26.0 * 18,
                        style: TextStyle(fontSize: 17),
                        textAlignVertical: TextAlignVertical.bottom,
                        specialTextSpanBuilder: MySpecialTextSpanBuilder(),
                        key: widget.textFieldKey,
                        keyboardType: TextInputType.text,
                        textCapitalization: TextCapitalization.none,
                        onEditingComplete: () {
                          widget.value(widget.textEditingController.text);
                          widget.textEditingController.clear();
                        },
                        maxLines: 5,
                        minLines: 1,
                        controller: widget.textEditingController,
                        focusNode: _focusNode,
                        decoration: const InputDecoration(
                            isDense: true, border: InputBorder.none),
                      ),
                    ),
            ),
            Row(
              children: [
                const SizedBox(width: 10),
                InkResponse(
                    onTap: () {
                      setState(() {
                        type = 1;
                      });
                      widget.biaoqing();
                    },
                    child: Image.asset(
                      "asset/message/expression.png",
                      width: 24,
                      height: 24,
                      fit: BoxFit.fill,
                    )),
                SizedBox(width: 10),
                InkResponse(
                    onTap: () {
                      setState(() {
                        type = 1;
                      });
                      widget.moreTap();
                    },
                    child: Image.asset(
                      "asset/message/more.png",
                      width: 24,
                      height: 24,
                      fit: BoxFit.fill,
                    )),
              ],
            )
          ],
        ),
      ),
    );
  }
}
